package Metier;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;


public class XmlType {

	
	static public void setParam(List<String> listParam)
	{
		SAXBuilder sxb = new SAXBuilder();
		try {
			 Document document = sxb.build(new File("config.xml"));//f.getParent()+"/config.xml"));
			 Element racine = document.getRootElement();
			 List<Element> listeElement = racine.getChildren();
			 for(int i=0;i<listeElement.size();i++)
			 {
				 if (listeElement.get(i).getChild("nom").getValue().equals(listParam.get(0)))
				 {
					 listeElement.get(i).getChild("url").setText(listParam.get(1));
					 listeElement.get(i).getChild("base").setText(listParam.get(2));
					 listeElement.get(i).getChild("port").setText(listParam.get(3));
				 }
			 }
			 
			 XMLOutputter out = new XMLOutputter();
			out.output(document, new PrintWriter("config.xml"));
			 
		} catch (Exception e) {
		}
		
	}
	
	static public List<List<String>>  getParam()
	{
		 SAXBuilder sxb = new SAXBuilder();
		 List<List<String>> listParam = new ArrayList<List<String>>();
		 try {
			 Document document = sxb.build(new File("config.xml"));//f.getParent()+"/config.xml"));
			Element racine = document.getRootElement();
			List<Element> listElement= racine.getChildren("Connexion");//getChildren("Mysql");
			for(int i=0;i<listElement.size();i++)
			{
				List <String> listAtt= new ArrayList<String>();
				Element element= listElement.get(i);
				
				List<Element> listAttribut = new ArrayList<Element>();
				listAttribut.add(element.getChild("nom"));
				listAttribut.add(element.getChild("url"));
				listAttribut.add(element.getChild("base"));
				listAttribut.add(element.getChild("port"));
				
				for (int j=0;j<listAttribut.size();j++)
				{
					
					if (listAttribut.get(j)!=null)
					{
						listAtt.add(listAttribut.get(j).getValue());
					}
					else
					{
						listAtt.add("");
					}
					
				}
				listParam.add(listAtt);
			}
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
			File f = new File(System.getProperty("java.class.path"));
			JOptionPane.showMessageDialog(null, e.getMessage()+" : "+f.getParent()+"/config.xml",
					"Connexion", JOptionPane.INFORMATION_MESSAGE);
		}
		return listParam;
	}
}
